
#ifndef _INFRARED_RECEIVE_H
#define _INFRARED_RECEIVE_H


#include "stdint.h"


#if 0

#define NUM_INFRARED_LEFT        0
#define NUM_INFRARED_RIGHT       1
#define NUM_INFRARED_BACK_LEFT   2
#define NUM_INFRARED_BACK_RIGHT  3

#else

#define NUM_INFRARED_LEFT        0
#define NUM_INFRARED_RIGHT       1
#define NUM_INFRARED_BACK_LEFT   3
#define NUM_INFRARED_BACK_RIGHT  2

#endif



#define BOOT_CODE_LENGTH_US       2000
#define BOOT_CODE_LENGTH_ERRO     200

#define BOOT_CODE_LENGTH_MAX_US   (BOOT_CODE_LENGTH_US + BOOT_CODE_LENGTH_ERRO)
#define BOOT_CODE_LENGTH_MIN_US   (BOOT_CODE_LENGTH_US - BOOT_CODE_LENGTH_ERRO)


#define BOOL_HIGH_LENGTH_US       1200
#define BOOL_HIGH_LENGTH_ERRO     200
#define BOOL_HIGH_LENGTH_MAX_US   (BOOL_HIGH_LENGTH_US + BOOL_HIGH_LENGTH_ERRO) 
#define BOOL_HIGH_LENGTH_MIN_US   (BOOL_HIGH_LENGTH_US - BOOL_HIGH_LENGTH_ERRO) 

#define BOOL_LOW_LENGTH_US        600
#define BOOL_LOW_LENGTH_ERRO      200

#define BOOL_LOW_LENGTH_MAX_US    (BOOL_LOW_LENGTH_US + BOOL_LOW_LENGTH_ERRO) 
#define BOOL_LOW_LENGTH_MIN_US    (BOOL_LOW_LENGTH_US - BOOL_LOW_LENGTH_ERRO)

#define INFRARED_DATA_LENGTH        9

#define INFRARED_SIGNAL_HOLD_TIME   40

#define INFRARED_AREA_NUM_A      0b00001000
#define INFRARED_AREA_NUM_B      0b00000100
#define INFRARED_AREA_NUM_C      0b00000010
#define INFRARED_AREA_NUM_D      0b00000001
#define INFRARED_AREA_NUM_E      0b00001100
#define INFRARED_AREA_NUM_F      0b00000110
#define INFRARED_AREA_NUM_G      0b00000011

#define INFRARED_AREA_NUM_H      0b00000111
#define INFRARED_AREA_NUM_I      0b00001110

#define INFRARED_AREA_NUM_J      0b00000101
#define INFRARED_AREA_NUM_K      0b00001010

#define INFRARED_AREA_NUM_Z      0b00001001
#define INFRARED_AREA_NULL       0
#define INFRARED_AREA_ERRO       0b00001111

#define READ_INFRARED_X1          gpio_input_bit_get(GPIOF,GPIO_PIN_2)
#define READ_INFRARED_X2          gpio_input_bit_get(GPIOF,GPIO_PIN_3)
#define READ_INFRARED_X3          gpio_input_bit_get(GPIOF,GPIO_PIN_4)
#define READ_INFRARED_X4          gpio_input_bit_get(GPIOF,GPIO_PIN_5)

#define AREA_COVER_REC_TIME     150

typedef enum
{
	BOOL_LOW   = 0,
	BOOL_HIGHT = 1,
	BOOT_CODE  = 2,	
	PULSE_INVALID = 9

}ENUM_INFRARED;


typedef struct
{
	int8_t   pulse_cnt;
	uint8_t  sample_start;
	uint8_t  sample_finish;
	
	uint16_t timercnt[2];
	
	int16_t  pulse_width[INFRARED_DATA_LENGTH];
	int8_t   pulse_value[INFRARED_DATA_LENGTH];
	
	uint8_t  timeout;
	uint16_t holdcnt;
	
	union
	{
		struct
		{
			uint8_t receive_x1 :1;
			uint8_t receive_x2 :1;
			uint8_t receive_x3 :1;
			uint8_t receive_x4 :1;
			
			uint8_t receive_x5 :1;
			uint8_t receive_x6 :1;
			uint8_t receive_x7 :1;
			uint8_t receive_x8 :1;
			
		}bit;
		
		struct
		{
			uint8_t trueform  :4;
			uint8_t compform  :4;
		
		}half;
		
		uint8_t all;
	
	}valid_value;
	
	uint8_t  trueform_old;
	uint8_t  trueform_history[4];
	uint64_t timestamp_now;
	uint64_t timestamp_old;
	
	uint8_t  position_value;

}STR_INFRARED;



extern void infrared_receive_init(void);
extern void infrared_position_check(void);


#endif
